package com.afollestad.materialdialogs.internal;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.ResourceTable;
import com.afollestad.materialdialogs.utils.DialogUtils;
import com.afollestad.materialdialogs.utils.ResUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

/**
 * @author Kevin Barry (teslacoil) 4/02/2015
 */
public class MDButton extends Text {
    private boolean stacked = false;

    private GravityEnum stackedGravity;

    private int stackedEndPadding;

    private Element stackedBackground;

    private Element defaultBackground;

    public MDButton(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MDButton(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        stackedEndPadding = ResUtil.getIntDimen(context, ResourceTable.Float_md_dialog_frame_margin);
        stackedGravity = GravityEnum.END;
    }

    /**
     * Set if the button should be displayed in stacked mode. This should only be called from
     * MDRootLayout's onMeasure, and we must be measured after calling this.
     *
     * @param force
     * @param stacked
     */
    /* package */ void setStacked(boolean stacked, boolean force) {
        if (this.stacked != stacked || force) {

            setForegroundGravity(
                    stacked ? (TextAlignment.VERTICAL_CENTER | stackedGravity.getGravityInt()) : TextAlignment.CENTER);
            //noinspection ResourceType
            setTextAlignment(stacked ? stackedGravity.getTextAlignment() : TextAlignment.CENTER);

            DialogUtils.setBackgroundCompat(this, stacked ? stackedBackground : defaultBackground);
            if (stacked) {
                setPadding(stackedEndPadding, getPaddingTop(), stackedEndPadding, getPaddingBottom());
            } /* Else the padding was properly reset by the drawable */

            this.stacked = stacked;
        }
    }

    void setStackedGravity(GravityEnum gravity) {
        stackedGravity = gravity;
    }

    public void setStackedSelector(Element d) {
        stackedBackground = d;
        if (stacked) {
            setStacked(true, true);
        }
    }

    public void setDefaultSelector(Element d) {
        defaultBackground = d;
        if (!stacked) {
            setStacked(false, true);
        }
    }
}
